import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/classesDatagrid.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerSchoolAdmin.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/sidebars/adminSidebar.dart';

class ClassesManagement extends StatefulWidget {
  static const String routeName = '/schooladmin/classes';

  @override
  State<ClassesManagement> createState() => _ClassesManagementState();
}

class _ClassesManagementState extends State<ClassesManagement> {
  final box = Hive.box('appconfig');
  final SchoolAdminController _adminController = Get.find();

  @override
  void initState() {
    bool isSchoolAdmin = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_SchoolAdmin;
    if (isSchoolAdmin) {
      _adminController.classItemsWithoutAll.clear();
      Future.delayed(Duration(milliseconds: 200), () {
        if (isSchoolAdmin) {
          _adminController.fetchSchoolAllClassesDetailsWithoutAll();
          _adminController.fetchSchoolTeachers();
        }
      });
    }
    super.initState();
  }

  @override
  void dispose() {
    _adminController.classItemsWithoutAll.clear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug("build schooladmin ClassesManagement");

    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.schoolAdminPageItems
        .indexWhere((page) => page.route == Get.currentRoute);
    final PageStyles _pageStyles = PageStyles();

    return Scaffold(
      appBar: isSmallScreen ? CustomAppBar(title: 'Class Management'.tr) : null,
      drawer: isSmallScreen ? SchoolAdminAppDrawer() : null,
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        child: Row(
          children: [
            Visibility(
                visible: !isSmallScreen,
                child: AdminSidebarX(
                    selectedIndex: _selectedSidebarItem,
                    isBigScreen: isBigScreen)),
            Expanded(
              child: Center(
                  child: _adminController.obx(
                (data) =>
                    ClassDataGrids(true, _adminController.classItemsWithoutAll),
                onEmpty:
                    ClassDataGrids(true, _adminController.classItemsWithoutAll),
              )),
            ),
          ],
        ),
      ),
    );
  }
}
